//$("body").append('<script src="../public/url_title.js" type="text/javascript"></script>');

var loc = location.href;

/** 根据loc 取值 **/
if(loc.split('?')[1]) {
  var locs = loc.split('?')[1].split('&');
  for(var i = 0; i < locs.length; i++) {
    if(locs[i].split('=')[0] == "type") {
      var type = locs[i].split('=')[1];
    }
    if(locs[i].split('=')[0] == "newsId") {
      var newsId = locs[i].split('=')[1];
    }
  }
}

var uto = {
  menu: function(a) {
    var menu = $("#menu"),
      dialog = $("#dialog"),
      menuHeight = menu.height();

    if(menuHeight == 0) {
      $(a).addClass("menu-on");
      menu.height(5 * 6 + "rem");
      dialog.show().on("click", function() {
        $(a).removeClass("menu-on");
        menu.height(0);
        $(this).hide();
      });
    } else {
      $(a).removeClass("menu-on");
      menu.height(0);
      dialog.hide();
    }
  },
  /**  界面初始化  **/
  init: function() {
    Api.scrollTop();
    /** 菜单栏 **/
    $("#menu-btn").on("click", function() {
      uto.menu(this);
    });

    /** copyright 年 **/
    $("#nowYear").text(new Date().getFullYear());
    /**  seo  **/
    $("#html")[0] ? Api.seo({
      "pageCode": $("#html").attr("data-key")
    }, function(data) {
      $("#description").attr("content", data.data.detail);
      $("#keywords").attr("content", data.data.keyWord);
    }) : "";
    /** 关闭下载app浮窗 **/
    $("#closedownload").on("click", function() {
      $("#appdownload").hide();
    });
  }
}
// var url_title = "http://192.168.3.37:8080/uto-official-site";
var url_title ="http://120.25.90.226:8080";
/** 常用method **/
var Api = {
  beforeSend: function(xhr) {

  },
  http: function(url, method, data, onSuccess) {
    $.ajax({
      url: url_title + url,
      type: method,
      data: data,
      success: function(data) {
        onSuccess(data);
      },
      error: function(a) {
        console.log(JSON.stringify(a));
      }
    })
  },
  /** 接口列表 **/
  /** 新闻列表 **/
  newsList: function(data, onSuccess) {
    this.http("/visitors/news/list", "get", data, onSuccess);
  },
  /** 新闻详情 **/
  newsInfo: function(data, onSuccess) {
    this.http("/visitors/news/view", "get", data, onSuccess);
  },
  // banner 列表
  bannerList: function(data, onSuccess) {
    this.http("/visitors/banner/list", "get", data, onSuccess, "");
  },
  // seo
  seo: function(data, onSuccess) {
    this.http("/visitors/keyword/view", "get", data, onSuccess, "");
  },
  /** 时间格式化 **/
  timeFormat: function(time, type) {
    var t = new Date(time);
    var tf = function(i) {
      return(i < 10 ? '0' : '') + i;
    }
    return type.replace(/yyyy|MM|dd|HH|mm|ss/g, function(a) {
      switch(a) {
        case 'yyyy':
          return tf(t.getFullYear());
          break;
        case 'MM':
          return tf(t.getMonth() + 1);
          break;
        case 'mm':
          return tf(t.getMinutes());
          break;
        case 'dd':
          return tf(t.getDate());
          break;
        case 'HH':
          return tf(t.getHours());
          break;
        case 'ss':
          return tf(t.getSeconds());
          break;
      }
    })
  },
  /*截取字符串*/
  str: function(s, len) {
      if(s.length > len) {
        s = s.substring(0, len) + '...';
        return s
      } else {
        return s
      }
    },
    /** 返回顶部 **/
  scrollTop: function() {
    /** 加载页面公用标签 **/
    var html = '<a href="javascript:void(0)" id="backTop"><img src="images/back.png" /></a>';
    $('body').append(html);
    var css = '<style>#backTop{position:fixed;bottom:6rem;right:1rem;width:3.5rem;display:none;transition: display 200ms linear;}</style>';
    $('head').append(css);

    $.fn.scrollTo = function(options) {
      var defaults = {
        toT: 0, //滚动目标位置
        durTime: 200, //过渡动画时间
        delay: 30, //定时器时间
        callback: null //回调函数
      };
      var opts = $.extend(defaults, options),
        timer = null,
        _this = this,
        curTop = _this.scrollTop(), //滚动条当前的位置
        subTop = opts.toT - curTop, //滚动条目标位置和当前位置的差值
        index = 0,
        dur = Math.round(opts.durTime / opts.delay),
        smoothScroll = function(t) {
          index++;
          var per = Math.round(subTop / dur);
          if(index >= dur) {
            _this.scrollTop(t);
            window.clearInterval(timer);
            if(opts.callback && typeof opts.callback == 'function') {
              opts.callback();
            }
            return;
          } else {
            _this.scrollTop(curTop + index * per);
          }
        };
      timer = window.setInterval(function() {
        smoothScroll(opts.toT);
      }, opts.delay);
      return _this;
    };

    $("#backTop").on('click', function() {
      $('body').scrollTo({
        toT: 0
      });
    })

    $(window).scroll(function(e) {
      $(window).scrollTop() > 0 ? $("#backTop").show() : $("#backTop").hide();
    })
  },
}

$(function() {
  uto.init();
});